package com.baselib.android.utils;


import android.util.Log;

import com.alibaba.fastjson.JSONArray;
import com.baselib.android.expection.BaseException;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GsonUtil {

    public static <T> T jsonToEntity(String json, Class<T> clazz) throws BaseException {
        try {
//             Log.e("--",json);

            Gson g = new Gson();
            T respones = g.fromJson(json, clazz);

//            Log.e("--OVER ",json);
            return respones;
        } catch (Exception e) {
            Logger.e(e);
            Log.e("gson com", "gson parse data error" + e.getLocalizedMessage());
            return null;
        }
    }


    public static <T> List<T> jsonToArrayEntity(String jsonArray, Class<T> clazz) throws BaseException {


        List<T> list = new ArrayList<T>();
        try {
            org.json.JSONArray ja = new org.json.JSONArray(jsonArray);
            for (int i = 0; i < ja.length(); i++) {
                list.add(jsonToEntity(ja.get(i).toString(), clazz));
            }
        } catch (Exception e) {
            Log.e("error--", e.getLocalizedMessage());
        }
        return list;

    }


    public static <T> String arrayToJson(List<T> clazz) {
        if (ListUtil.isNullOrEmpty(clazz)) {
            return "";
        }
        return JSONArray.toJSONString(clazz);
    }


    /**
     * 将实体转换成json
     *
     * @param clazz
     * @return
     */
    public static <T> String entityToJson(T clazz) throws BaseException {
//        try {
//            return JSONObject.toJSONString(clazz);
//        } catch (Exception e) {
//            Log.e("-------","CoreRevier="+e.getLocalizedMessage());
//            throw new BaseException("实体转换成json");
//        }

        try {
            Gson g = new Gson();
            String json = g.toJson(clazz);
            return json;
        } catch (Exception e) {
            Log.e("GSON", e.getLocalizedMessage());
            return "";
        }
    }

    /**
     * 实体转换成 hasmap
     *
     * @param t
     * @return
     */
    public static <T> HashMap EntityToHas(T t) {
        String json = entityToJson(t);
        HashMap hm = jsonToHas(json);
        return hm;
    }

    /**
     * 将 json 转换成hasmap
     *
     * @param json
     * @return
     */
    public static HashMap jsonToHas(String json) {
        try {
            Gson gson = new Gson();
            HashMap hm = gson.fromJson(json, HashMap.class);
            return hm;
        } catch (Exception e) {
            // TODO: handle exception
            return new HashMap();
        }

    }


    /**
     * 将 json 转换成hasmap
     *
     * @param
     * @return
     */
    public static String HasToJson(Map<String, Object> hashMap) {
        try {
            Gson gson = new Gson();
            String hm = gson.toJson(hashMap);
            return hm;
        } catch (Exception e) {
            // TODO: handle exception

        }
        return "";
    }


}
